//
//  UUCarVC.m
//  UUCharterSupplier
//
//  Created by ZhangAo on 23/01/2018.
//  Copyright © 2018 UUCharter. All rights reserved.
//

#import "UUCarVC.h"
#import "UUResourceManager.h"
#import "UUCarManager.h"
#import "UUCarStateListVC.h"
#import "UUCarEditVC.h"
#import "UUPermissionManager.h"
#import <WZLBadge/WZLBadgeImport.h>

@interface UUCarVC ()

@property (nonatomic, weak) UUPagingMenuController *menuController;
@property (nonatomic, weak) DKPagingMenuItem *deniedItem;

@end

@implementation UUCarVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if ([GetPermissionManager() checkPermission:UUPermissionFlagCarReadWrite]) {
        self.navigationItem.rightBarButtonItem = [UIBarButtonItem barButtonItemWithTitle:GetResourceManager().addIconText
                                                                                  target:self
                                                                                selector:@selector(addCar:)];
    }
    
    NSMutableArray<DKPagingMenuItem *> *tabPageItems = [NSMutableArray arrayWithCapacity:3];
    
    DKPagingMenuItem *verifiedItem = [[UUPagingMenuItem alloc] initWithTitle:@"已审核"
                                                          menuViewController:[UUCarStateListVC listWithStatus:UUCarReviewStatusApproval]];
    [tabPageItems addObject:verifiedItem];
    
    DKPagingMenuItem *verifyItem = [[UUPagingMenuItem alloc] initWithTitle:@"待审核"
                                                        menuViewController:[UUCarStateListVC listWithStatus:UUCarReviewStatusInReview]];
    [tabPageItems addObject:verifyItem];
    
    DKPagingMenuItem *deniedItem = [[UUPagingMenuItem alloc] initWithTitle:@"已拒绝"
                                                        menuViewController:[UUCarStateListVC listWithStatus:UUCarReviewStatusRejected]];
    [tabPageItems addObject:deniedItem];
    
    UUPagingMenuController *menuController = [[UUPagingMenuController alloc] initWithItems:tabPageItems];
    menuController.scrollView.scrollEnabled = NO;
    [self addChildViewController:menuController];
    [self.view addSubview:menuController.view];
    [menuController.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_topLayoutGuideBottom);
        make.left.right.bottom.mas_equalTo(0);
    }];
    
    self.deniedItem = deniedItem;
    self.menuController = menuController;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    NSInteger count = [GetCarManager() fetchCarsForStatus:UUCarReviewStatusRejected].count;
    [self.deniedItem.view showBadgeWithStyle:WBadgeStyleNumber
                                       value:count
                               animationType:WBadgeAnimTypeNone];
}

#pragma mark - Private methods

- (IBAction)addCar:(id)sender {
    UUCarEditVC *editVC = [UUCarEditVC instanceOfAppropriate];
    editVC.editable = YES;
    
    WEAK_SELF();
    [editVC setCarDidAddBlock:^{
        [weakSelf.menuController setSelectedIndex:1];
    }];
    [self.navigationController pushViewController:editVC animated:YES];
}

@end
